package com.sailors.common.task.provider.faced.controller;

import com.alibaba.fastjson.JSON;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskFailedExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.execute.RefreshProcessExecuteDto;
import com.sailors.common.task.provider.infra.design.common.rest.BaseResponse;
import com.sailors.common.task.provider.infra.design.common.rest.Bru;
import com.sailors.common.task.provider.infra.system.constant.Topics;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.DescribeTopicsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Slf4j
@RequestMapping("/debug")
@RestController
@RefreshScope
public class DebugController {

    @Value("${spring.application.name:nope}")
    private String applicationName;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @PostMapping("/application")
    public BaseResponse<String> application() throws ExecutionException, InterruptedException {
//        RefreshProcessExecuteDto refreshProcessExecuteDto = new RefreshProcessExecuteDto();
//        refreshProcessExecuteDto.setCreateBy("cyj");
//        refreshProcessExecuteDto.setProcess(new BigDecimal(51));
//        refreshProcessExecuteDto.setTaskId(1595359570648879106L);
//        kafkaTemplate.send(Topics.TASK_PROCESS, JSON.toJSONString(refreshProcessExecuteDto));
//
//        FileTaskFailedExecuteDto fileTaskFailedExecuteDto = new FileTaskFailedExecuteDto();
//        fileTaskFailedExecuteDto.setReason("cyj");
//        fileTaskFailedExecuteDto.setReasonUrl(new BigDecimal(51).toString());
//        fileTaskFailedExecuteDto.setTaskId(1595359570648879106L);
//        kafkaTemplate.send(Topics.FAILED_FILE_TASK, JSON.toJSONString(fileTaskFailedExecuteDto));
//        Map<String, Object> props = new HashMap<>();
//        //配置Kafka实例的连接地址
//        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "42.193.226.99:9092");
//        AdminClient adminClient = AdminClient.create(props);
//        DescribeTopicsResult result = adminClient.describeTopics(
//                Arrays.asList(Topics.TASK_PROCESS, Topics.FAILED_FILE_TASK));
        return Bru.success(applicationName);
    }
}
